//封装获取分类数据的逻辑函数
import {ref,onMounted} from 'vue'
import { categoryGetCategoryDetail } from '@/apis/category'
import { useRoute ,onBeforeRouteUpdate} from 'vue-router'
export const useCategory = ()=>{
  const categoryDetail = ref({})
  const route = useRoute()
  const getCategoryData = async (id = route.params.id)=>{
    const res = await categoryGetCategoryDetail(id)
    categoryDetail.value = res.data.result
  }
  // 组件挂载时获取分类数据
  onMounted(()=>getCategoryData())
  //当路由变化时,重新获取分类数据。
  onBeforeRouteUpdate((to)=>{
    getCategoryData(to.params.id)
  })
  return {categoryDetail}
}
